﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cecbData;
using System.Net.Mail;
using System.IO;
using System.Net.Mime;

namespace cecbClasses.Data_Adapters
{
    public class ActivationCodeEmail
    {
        public ActivationCodeEmail() 
        {
            cecbContext = new DSSEntities();
        }
         
        DSSEntities cecbContext;


        public List<Projects> getProjectDetails()
        {

            IQueryable<Projects> Query =
                  from c in cecbContext.Projects
                  select c;

            List<Projects> SelectedEmpList = Query.ToList();

            return SelectedEmpList;

        }

        public List<Members> getMemberRef()
        {
            IQueryable<Members> Query =
                  from c in cecbContext.Members
                  // where c.ProductCode == ProductCodeTextBox.Text.Trim()
                  select c;

            List<Members> SelectedEmpList = Query.ToList();

            return SelectedEmpList;

        }

        public String getMemberRefByName(String name)
        {
            var Ref = (from c in cecbContext.Members
                       where c.mem_name == name
                       select c).First();

            String memRef = Ref.mem_reference.ToString();

            return memRef;
        }

        public List<Members> getAllMemberNames()
        {
            IQueryable<Members> Query =
            from c in cecbContext.Members
            select c;

            List<Members> AllfNames = Query.ToList();

            return AllfNames;
        }

        public List<Projects> getAllProjectNames()
        {
            IQueryable<Projects> Query =
            from c in cecbContext.Projects
            select c;

            List<Projects> AllfNames = Query.ToList();

            return AllfNames;
        }

        public String getTeamRefByProRef(String pro_reference)
        {
            String memRef = "";
            try
            {
                var Ref = (from c in cecbContext.Projects
                           where c.proj_reference == pro_reference
                           select c).First();

              memRef = Ref.proj_name.ToString();  // CHNAGED COX OF AN ERRE

                
            }
            catch (Exception)
            {
                
            }
            return memRef;
        }

      //  public List<Members> getTeamMembers(String tem_reference)
    //    {
           
            //IQueryable<Members> quary = from c in cecbContext.Projects
            //            join b in cecbContext.TeamMembers on c.proj_team equals b.team_reference
            //           join d in cecbContext.Members on b.mem_reference equals d.mem_reference
            //            select new
            //            {
            //                d.mem_reference
            //            };

            //return quary.ToList();
           //List<Members> SelectedEmpList = new quary.ToList();

        //    return SelectedEmpList;

             //var query = from c in cecbContext.Projects
             //   join b in cecbContext.TeamMembers on c.proj_team equals b.team_reference
             //   join d in cecbContext.Members on b.mem_reference equals d.mem_reference
             //   where c.proj_reference == tem_reference
             //   select d.mem_reference;
             //return query.Select(mem_reference => new Members { mem_reference = b.mem_reference }).ToList();
    //    }

        public List<Members> getMemberNameByRef(String reference)
        {
            IQueryable<Members> Query =
                  from c in cecbContext.Members
                  where c.mem_reference == reference
                  select c;

            List<Members> SelectedEmpList = Query.ToList();

            return SelectedEmpList;
        }


        //public List<Member_Emails> getMemberEmailByRef(String reference)
        //{
        //    IQueryable<Member_Emails> Query =
        //          from c in cecbContext.Member_Emails
        //          where c.mem_reference == reference
        //          select c;

        //    List<Member_Emails> SelectedEmpEmailList = Query.ToList();

        //    return SelectedEmpEmailList;

        //}

        public string genarateRandomUsername(int Size)
        {
            Random ran = new Random();
            string input = "abcdefghijklmnopqrstuvwxyz";
            StringBuilder builder = new StringBuilder();
            char ch;
            for (int i = 0; i < Size; i++)
            {
                ch = input[ran.Next(0, input.Length)];
                builder.Append(ch);
            }
            return builder.ToString();
        }

        public string genarateRandomPassword(int Size)
        {
            Random ran = new Random();
            string input = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            StringBuilder builder = new StringBuilder();
            char ch;
            for (int i = 0; i < Size; i++)
            {
                ch = input[ran.Next(0, input.Length)];
                builder.Append(ch);
            }
            return builder.ToString();
        }


        public bool genarateEmail(String from, String to, String cc, String displayName, String password, String subjet, String body)
        {
            bool EmailIsSent = false;

            MailMessage m = new MailMessage();
            SmtpClient sc = new SmtpClient();
            try
            {
                m.From = new MailAddress(from, displayName);
                m.To.Add(new MailAddress(to, displayName));
                // m.CC.Add(new MailAddress("sliititb1@gmail.com", "Display name CC"));

                m.Subject = subjet;
                m.IsBodyHtml = true;
                m.Body = body + genarateRandomPassword(10);



                //FileStream fs = new FileStream("D:\\devided.txt",
                //                   FileMode.Open, FileAccess.Read);
                //Attachment a = new Attachment(fs, "devided.txt",
                //                   MediaTypeNames.Application.Octet);
                //m.Attachments.Add(a);

                //string str = "<html><body><h1>Picture</h1><br/><img src=\"cid:image1\"></body></html>";
                //AlternateView av =
                //             AlternateView.CreateAlternateViewFromString(str,
                //             null,MediaTypeNames.Text.Html);
                //LinkedResource lr = new LinkedResource("D:\\x.jpg",
                //             MediaTypeNames.Image.Jpeg);
                //lr.ContentId = "image1";
                //av.LinkedResources.Add(lr);
                //m.AlternateViews.Add(av);

                sc.Host = "smtp.gmail.com";
                sc.Port = 587;
                sc.Credentials = new
                System.Net.NetworkCredential(from, password);
                sc.EnableSsl = true;
                sc.Send(m);

                EmailIsSent = true;
               
                
            }
            catch (Exception ex)
            {
                EmailIsSent = false;
            }

            return EmailIsSent;
        }


        public bool genarateEmailToAll(String from, String to ,   String cc, String displayName, String password, String subjet, String body , String filePath , String memName  )
        {
            bool EmailIsSent = false;

             
                MailMessage m = new MailMessage();
                SmtpClient sc = new SmtpClient();
                try
                {
                    m.From = new MailAddress(from, displayName);
                    m.To.Add(new MailAddress(to, displayName));
                    m.CC.Add(new MailAddress("sliititb1@gmail.com", displayName+" CC"));

                    m.Subject = subjet;
                    m.IsBodyHtml = true;
                    m.Body = body;



                    try
                    {
                        FileStream fs = new FileStream(filePath + @"\" + memName + ".xls",
                                                       FileMode.Open, FileAccess.Read);
                        Attachment a = new Attachment(fs, memName + ".xls",
                                            MediaTypeNames.Application.Octet);
                        m.Attachments.Add(a);
                         
                    }
                    catch (Exception)
                    {
                        EmailIsSent = false;
                        return EmailIsSent;
                        
                    }

                    //string str = "<html><body><h1>Picture</h1><br/><img src=\"cid:image1\"></body></html>";
                    //AlternateView av =
                    //             AlternateView.CreateAlternateViewFromString(str,
                    //             null,MediaTypeNames.Text.Html);
                    //LinkedResource lr = new LinkedResource("D:\\x.jpg",
                    //             MediaTypeNames.Image.Jpeg);
                    //lr.ContentId = "image1";
                    //av.LinkedResources.Add(lr);
                    //m.AlternateViews.Add(av);

                    sc.Host = "smtp.gmail.com";
                    sc.Port = 587;
                    sc.Credentials = new
                    System.Net.NetworkCredential(from, password);
                    sc.EnableSsl = true;
                    sc.Send(m);

                    

                }
                catch (Exception ex)
                {
                    EmailIsSent = false;
                }
            
            
           

            return EmailIsSent;
        }


    }
}
